package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Description :订单30分钟后自动关单的监听器,消费者
 * @Author : mayp
 * @Date: 2021-02-12 17:03
 */
// 监听订单的释放队列，能到这个队列里面的消息，都是30分钟后过来的
@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloneListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单"+entity.getOrderSn());
        try {
            // 尝试关单，如果一切正常，就回复ACK机制
            orderService.cloneOrder(entity);
            // 可以手动调用支付宝进行收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            // 如果出现异常，消息就拒绝，让它重新回到消息队列中l
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
